package com.sphd.emanu.activity.topmanagers;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sphd.emanu.R;
import com.sphd.emanu.activity.staff.StaffActivity;
import com.sphd.emanu.adapter.TopmanagersAdapter;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.mvp.MVPBaseActivity;
import com.sphd.emanu.permission.Moudle;
import com.sphd.emanu.pojo.login.User;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 高管管理界面
 *
 * @Author Hu
 * @Time 2017/4/5 -- 16:44.
 * @Email 87683202@qq.com
 */
public class TopmanagersActivity extends MVPBaseActivity<TopmanagersContract.View, TopmanagersPresenter> implements TopmanagersContract.View {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.my_recycler)
    RecyclerView myRecycler;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    Intent intent;
    Bundle bundle;
    User user;
    TopmanagersAdapter adapter;
    List<Moudle> moudles;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_topmanagers);
        ButterKnife.bind(this);
        initView();
        initAdapter();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(v -> finish());
        mySwf.setOnRefreshListener(() -> {
            addData();
        });
        myRecycler.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {

                User userstaff = (User) adapter.getItem(position);
                bundle.putSerializable(Constant.USER_STAFF, userstaff);
                bundle.putInt(Constant.TYPE, 2);
                intent.putExtras(bundle);
                intent.setClass(context, StaffActivity.class);
                startActivityForResult(intent,Constant.REQUEST_CODE);
            }
        });
    }

    private void initAdapter() {
        setRefresh(mySwf);
        setRecy(myRecycler);
        adapter = new TopmanagersAdapter(R.layout.item_topmanagers, new ArrayList<User>());
        adapter.setEmptyView(emptyView);
        myRecycler.setAdapter(adapter);
    }

    private void initView() {
        setTitleBar(toolbar, getString(R.string.title_activity_topmanagers));
        intent = getIntent();
        bundle = intent.getExtras();
        moudles= (List<Moudle>) bundle.getSerializable(Constant.PERMISSION_LIST);
        user = (User) bundle.getSerializable(Constant.USER_ENTITY);
        addData();
    }

    private void addData() {
        showProgressDialog();
        mPresenter.selectManageUser(user.getOid());
    }

    @Override
    public void setListBean(List<User> list) {
        dissProgressDialog();
        adapter.setNewData(list);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==Constant.REQUEST_CODE){
            addData();
        }
    }
}
